#include "pkg.h"
#include "dict.h"
#include "attrib.h"

pkg_p pkg_new() {
	dict_p pkg = dict_new();
	return (pkg_p)pkg;
}

void pkg_free(pkg_p pkg) {
	dict_free((dict_p)pkg);
}

void pkg_set(pkg_p pkg, char *key, char *value) {
	dict_set_key((dict_p)pkg, key, value);
}

char *pkg_get(pkg_p pkg, char *key) {
	dict_p dict = (dict_p)pkg;
	attrib_p tmp;

	dict_to_first(dict);
	tmp = dict_find_next_key(dict, key);

	if (tmp)
		return attrib_get_value(tmp);
	return NULL;
}

int pkg_has(pkg_p pkg, char *key, char *value) {
	dict_to_first((dict_p)pkg);
	if (dict_find_next_match((dict_p)pkg, key, value))
		return 1;
	return 0;
}

void pkg_add(pkg_p pkg, char *key, char *value) {
	dict_add_new((dict_p)pkg, key, value);
}

list_p pkg_get_all(pkg_p pkg, char *key) {
	return dict_find_all_keys((dict_p)pkg, key);
}

void pkg_set_name(pkg_p pkg, char *name) {
	pkg_set(pkg, "name", name);
}

char *pkg_get_name(pkg_p pkg) {
	return pkg_get(pkg, "name");
}

void pkg_set_version(pkg_p pkg, char *version) {
	pkg_set(pkg, "version", version);
}

char *pkg_get_version(pkg_p pkg) {
	return pkg_get(pkg, "version");
}

void pkg_set_release(pkg_p pkg, char *release) {
	pkg_set(pkg, "release", release);
}

char *pkg_get_release(pkg_p pkg) {
	return pkg_get(pkg, "release");
}

void pkg_add_file(pkg_p pkg, char *file) {
	if (!pkg_has_file(pkg, file))
		pkg_add(pkg, "file", file);
}

list_p pkg_get_files(pkg_p pkg) {
	return pkg_get_all(pkg, "file");
}

int pkg_has_file(pkg_p pkg, char *file) {
	return pkg_has(pkg, "file", file);
}

